{% if response.HasType -%}
{%     if response.IsFile -%}
{%         if response.IsSuccess -%}
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, {% if InjectHttpClient or DisposeHttpClient == false %}null{% else %}client_{% endif %}, response_);
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
return fileResponse_;
{%         else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
{%         endif -%}
{%     elsif response.IsPlainText -%}
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = ({{ response.Type }})System.Convert.ChangeType(responseData_, typeof({{ response.Type }}));
{%         if response.IsSuccess -%}
{%             if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, result_);
{%             else -%}
return result_;
{%             endif -%}
{%         else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, responseData_, headers_, result_, null);
{%         endif -%}
{%     else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
{%         if response.IsNullable == false -%}
if (objectResponse_.Object == null)
{
    throw new {{ ExceptionClass }}("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
{%         endif -%}
{%         if response.IsSuccess -%}
{%             if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, objectResponse_.Object);
{%             else -%}
return objectResponse_.Object;
{%             endif -%}
{%         endif -%}
{%         if response.IsSuccess == false -%}
{%             if response.InheritsExceptionSchema -%}
var responseObject_ = objectResponse_.Object != null ? objectResponse_.Object : new {{ response.Type }}();
responseObject_.Data.Add("HttpStatus", status_.ToString());
responseObject_.Data.Add("HttpHeaders", headers_);
responseObject_.Data.Add("HttpResponse", objectResponse_.Text);
{%                 if WrapDtoExceptions -%}
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, responseObject_);
{%                 else -%}
throw responseObject_;
{%                 endif -%}
{%             else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
{%             endif -%}
{%         endif -%}
{%     endif -%}
{% elsif response.IsSuccess -%}
{%     if operation.HasResultType -%}
{%         if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
{%         else -%}
return {{ operation.UnwrappedResultDefaultValue }};
{%         endif -%}
{%     else -%}
{%         if operation.WrapResponse -%}
return new {{ ResponseClass }}(status_, headers_);
{%         else -%}
return;
{%         endif -%}
{%     endif -%}
{% else -%}{% comment %} implied: `if !response.HasType` so just read it as text {% endcomment %}
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, responseText_, headers_, null);
{% endif %}